<?php

class Admin_GroupsController extends Zend_Controller_Action {

    // Mang tham so nhan duoc o moi Action
    protected $_arrParam;
    protected $_page = 1;
    // Duong dan cua Action chinh
    protected $_actionMain;

    public function init() {
        // Mang tham so nhan duoc o moi Action
        $this->_arrParam = $this->_request->getParams();  
        
        //check permissions
        $auth = Zend_Auth::getInstance();
        $identity = $auth->getIdentity();
        $group_id = $identity->group;
        $groupsModel = new Admin_Model_Groups();
        $group_info = $groupsModel->fetchRow('id=' . $group_id)->toArray();
        $permissions = unserialize($group_info['permissions']);        
        if (!$group_info['admin'] || (!in_array($this->_arrParam ['action'], $permissions[ucfirst($this->_arrParam ['controller'])])&& $this->_arrParam ['action'] !='index')) {
            $this->_redirect('access_denied');
            die();
        }
              
        $this->_page = $this->_arrParam ['page'];
        if ($this->_page == '' || $this->_page <= 0) {
            $this->_page = 1;
        }

        // Duong dan cua Action chinh
        $this->_actionMain = '/' . $this->_arrParam ['module'] . '/' . $this->_arrParam ['controller'] . '/dashboard/page/' . $this->_page;
    }

    /**
     * function Index
     */
    public function indexAction(){
        $this->_redirect($this->_actionMain);
    }
    
    public function dashboardAction() {
        $translate = Zend_Registry::get('Zend_Translate');

        $this->view->title = $translate->_("TITLE_ADMIN_GROUPS_INDEX");

        $this->view->headTitle($this->view->title);

        $groupsModel = new Admin_Model_Groups();

        $listGroups = $groupsModel->fetchGroup();

        $paginator = Zend_Paginator::factory($listGroups);

        $paginator->setItemCountPerPage(NUM_PER_PAGE);

        $paginator->setCurrentPageNumber($this->_page);

        $this->view->page = $this->_page;

        $this->view->paginator = $paginator;
    }

    public function deleteitemsAction() {
        $this->_helper->layout()->disableLayout();

        $groupsModel = new Admin_Model_Groups();

        if ($this->_request->isPost()) {

            $item = $this->_arrParam ['cid'];

            foreach ($item as $k => $v) {

                $groupsModel->deleteGroup($v);
            }
        }

        $this->_redirect($this->_actionMain);
    }

    public function activeAction() {
        $this->_helper->layout()->disableLayout();

        $type = $this->_request->getParam('type', 1);

        $id = $this->_request->getParam('id', 0);

        if ($type > 0) {

            $type = 1;
        } else {

            $type = 0;
        }

        $groupsModel = new Admin_Model_Groups ();

        $groupsModel->update(array(
            'active' => $type
                ), 'id=' . $id);

        $this->_redirect($this->_actionMain);
    }

    public function insertAction() {
        $messages = array();

        $translate = Zend_Registry::get('Zend_Translate');

        $this->view->title = $translate->_("TITLE_ADMIN_GROUPS_INSERT");

        $this->view->headTitle($this->view->title);

        $form = new Admin_Form_Groups_UpdateGroups();

        $back = $form->getElement('back');

        $back->setAttrib('onclick', "window.location='" . $this->getFrontController()->getBaseUrl() . "/admin/groups/dashboard/page/" . $this->_page . "';");

        if ($this->_request->isPost()) {

            if ($form->isValid($_POST)) {

                $groupsModel = new Admin_Model_Groups ();

                $status = $_POST ['active'];

                $admin = $_POST ['admin'];

                $name = ucwords(trim($form->getValue('name')));

                $description = trim($form->getValue('description'));

                $groupId = $groupsModel->insertGroups($name, $description, $status, $admin);

                $messages [] = array(
                    'text' => $translate->_("ADMIN_TEXT_MESSAGE_ADD_GROUPS_SUCCESS"),
                    'type' => 'success'
                );
            }
        }

        $this->view->message = $messages;

        $this->view->form = $form;
    }

    public function updateAction() {
        $translate = Zend_Registry::get('Zend_Translate');

        $this->view->title = $translate->_("TITLE_ADMIN_GROUPS_UPDATE");

        $this->view->headTitle($this->view->title);

        $form = new Admin_Form_Groups_UpdateGroups();

        $back = $form->getElement('back');

        $back->setAttrib('onclick', "window.location='" . $this->getFrontController()->getBaseUrl() . "/admin/groups/dashboard/page/" . $this->_page . "';");

        $id_group = $this->_getParam('id', 0);

        $groupsModel = new Admin_Model_Groups;

        $messages = array();

        if ($this->_request->isPost()) {

            if ($form->isValid($_POST)) {

                $status = $_POST ['active'];

                $admin = $_POST ['admin'];

                $name = ucwords(trim($form->getValue('name')));

                $description = trim($form->getValue('description'));

                $status = $_POST ['active'];

                $processGroup = $groupsModel->updateGroup($id_group, $name, $description, $status, $admin);

                if ($processGroup) {

                    $messages [] = array(
                        'text' => $translate->_("ADMIN_TEXT_MESSAGE_UPDATE_GROUPS_SUCCESS"),
                        'type' => 'success'
                    );
                } else {

                    $messages [] = array(
                        'text' => $translate->_("ADMIN_TEXT_MESSAGE_UPDATE_GROUPS_FAILD"),
                        'type' => 'errormsg'
                    );
                }
            }
        } else {

            $data = $groupsModel->fetchRow('id=' . $id_group);

            $data = $data->toArray();

            if (count($data) > 0) {

                $form->populate($data);
            } else {

                $this->_redirect($this->_actionMain);
            }
        }

        $this->view->message = $messages;

        $this->view->form = $form;
    }

    public function deleteAction() {
        $translate = Zend_Registry::get('Zend_Translate');

        $this->view->title = $translate->_("TITLE_ADMIN_GROUPS_DELETE");

        $this->view->headTitle($this->view->title);

        $id = $this->_getParam('id', 0);

        $groupsModel = new Admin_Model_Groups;

        if ($this->getRequest()->isPost()) {

            $del = $this->getRequest()->getPost('del');

            if ($del == 'Yes') {

                $process = $groupsModel->deleteGroup($id);
            }

            $this->_redirect($this->_actionMain);
        } else {

            $data = $groupsModel->fetchRow('id=' . $id);

            $this->view->group = $data->toArray();
        }
    }

    public function permissionsAction() {
        $translate = Zend_Registry::get('Zend_Translate');

        $this->view->title = $translate->_("TITLE_ADMIN_GROUPS_PERMISSIONS");

        $this->view->headTitle($this->view->title);
        $form = new Admin_Form_Groups_Permissions();
        $back = $form->getElement('back');
        $back->setAttrib('onclick', "window.location='" . $this->getFrontController()->getBaseUrl() . "/admin/groups/dashboard/page/" . $this->_page . "';");

        $groupsModel = new Admin_Model_Groups();
        $modulesModel = new Admin_Model_Modules();
        $list_modules = $modulesModel->fetchAll()->toArray();

        $id_group = $this->_getParam('id', 0);

        $messages = array();
        if ($this->_request->isPost()) {
            if ($form->isValid($_POST)) {
                $post_data = $_POST;
                $groupsModel->updatePermissions($id_group, serialize($post_data));
                foreach ($list_modules as $key => $value) {
                    $list_modules[$key]['actions'] = explode(',', $list_modules[$key]['actions']);
                    foreach ($list_modules[$key]['actions'] as $action_value) {
                        $list_actions[$action_value] = $action_value;
                    }
                    $get_value = array();
                    foreach ($post_data as $key_post => $value_post) {
                        if ($key_post == $value['name']) {
                            $get_value = $value_post;
                            break;
                        }
                    }
                    $multiCheckbox = $form->createElement('multiCheckbox', $value['name'] . '[]', array('label' => $value['name'], 'class' => 'cb_permission',
                        'value' => $get_value, // select these 2 values
                        'multiOptions' => $list_actions, 'separator' => ''));
                    $form->addElement($multiCheckbox);
                }
            }
        } else {
            $data = $groupsModel->fetchRow('id=' . $id_group);
            $data = $data->toArray();
            if (count($data) > 0) {
                $data['permissions'] = unserialize($data['permissions']);
                foreach ($list_modules as $key => $value) {
                    $list_modules[$key]['actions'] = explode(',', $list_modules[$key]['actions']);
                    $list_actions = array();
                    foreach ($list_modules[$key]['actions'] as $action_value) {
                        $list_actions[$action_value] = $action_value;
                    }
                    $get_value = array();
                    foreach ($data['permissions'] as $key_post => $value_post) {
                        if ($key_post == $value['name']) {
                            $get_value = $value_post;
                            break;
                        }
                    }
                    $multiCheckbox = $form->createElement('multiCheckbox', $value['name'] . '[]', array('label' => $value['name'], 'class' => 'cb_permission',
                        'value' => $get_value, // select these 2 values
                        'multiOptions' => $list_actions, 'separator' => ''));
                    $form->addElement($multiCheckbox);
                }
            } else {
                $this->_redirect($this->_actionMain);
            }
        }

        $this->view->form = $form;
        $this->view->modules = $list_modules;
        $this->view->message = $messages;
    }

}